//
//  LWVisitorLoginView.swift
//  Microblog
//
//  Created by Roman on 17/1/6.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit
import SnapKit

//所有协议都遵守基协议
@objc protocol LWVisitorLoginViewDelegate: NSObjectProtocol {
    
  @objc optional func userWillLogin()
    
    func userWillRegister()
}

class LWVisitorLoginView: UIView {
    
    //代理属性
    weak var delegate: LWVisitorLoginViewDelegate?

    //通过该方法设置方可视图界面
    func updateInfo(imageName: String, title: String ,isHome: Bool = false ) {
        //修改图片和提示文字
        self.tipLabel.text = title
        self.criClenIcon.image = UIImage(named: imageName)
        if isHome {
            startAnimation()
        }else{
            //不是首页
            //设置boll 进行隐藏 ,还有遮罩的隐藏
            self.largeHouse.isHidden = true
            self.backView.isHidden = true
        
        }
        
    }
    
    private func startAnimation() {
        //实例化动画对象
        let animatior = CABasicAnimation(keyPath: "transform.rotation")
        //动画的基本设置
        animatior.repeatCount = MAXFLOAT
        animatior.duration = 3
        animatior.toValue = M_PI * 2
        //当动画结束,或者页面失去活跃的时候,动画不从图层中移除
        animatior.isRemovedOnCompletion = false
        
        //将动画对象添加到图层
        criClenIcon.layer.add(animatior, forKey: nil)
    }
    
    
    //自定义访客视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //XIB 加载就报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK :设置UI界面
    private func setupUI(){
        //添加子视图
        addSubview(criClenIcon)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registBtn)
        
        /*
         苹果自带的约束.
        criClenIcon.translatesAutoresizingMaskIntoConstraints = false
        //创建约束对象 并将约束对象添加到父控件上
        self.addConstraint(NSLayoutConstraint(item: criClenIcon, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: criClenIcon, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        */
        //使用第三方框架来实现布局
        criClenIcon.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        //房子布局
        largeHouse.snp.makeConstraints { (make) in
            make.center.equalTo(criClenIcon)
        }
        //提示文字
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(criClenIcon.snp.bottom).offset(30)
            make.centerX.equalTo(criClenIcon)
        }
        //设置button 布局
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.leading.equalTo(tipLabel)
            make.width.equalTo(70)
        }
        registBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.trailing.equalTo(tipLabel)
            make.width.equalTo(70)
        }
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(criClenIcon)
        }
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        //监听按钮的点击事件
        
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: UIControlEvents.touchUpInside)
        
        registBtn.addTarget(self, action: #selector(registerBtnClick), for: UIControlEvents.touchUpInside)
        
    }
    func loginBtnDidClick()  {
        delegate?.userWillLogin?()
    }
    func registerBtnClick()  {
        delegate?.userWillRegister()
    }
    //大房子
    lazy var largeHouse:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //圈圈
    lazy var criClenIcon:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //提示文字
    lazy var tipLabel: UILabel = {
        let lab = UILabel(text: "关注一些人,看看有什么惊喜关注一些人,看看有什么惊喜关注一些人,看看有什么惊喜", fontSize: 14, textColor: UIColor.gray)
        lab.preferredMaxLayoutWidth = 220
        lab.textAlignment = .center
        lab.numberOfLines = 0
        return lab
    }()
    
//    //设置按钮
    lazy var loginBtn: UIButton = {
       // let btn = UIButton(text: "登录", fontSize: 14, textColor: UIColor.orange)
//        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        let btn = UIButton(backImageName: "common_button_white", text: "登录", fontSize: 14, textColor: UIColor.orange)
        
        return btn
    }()
    
    lazy var registBtn: UIButton = {
       // let btn = UIButton(text: "注册", fontSize: 14, textColor: UIColor.orange)
       // btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        let btn = UIButton(backImageName: "common_button_white", text: "注册", fontSize: 14, textColor: UIColor.darkGray)
        return btn
    }()
    
    //设置遮罩
    lazy var backView: UIView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    
    

}
